﻿using System;
using System.Windows.Data;

namespace FSquirrel.Helpers
{
    [ValueConversion(typeof(TimeSpan), typeof(String))]
    public class DBNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
            if (DBNull.Value.Equals(value))
                return string.Empty;
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = value as string;
            if (String.IsNullOrEmpty(str))
                return DBNull.Value; // returns DBNull.Value

            TimeSpan result = new TimeSpan();

            TimeSpan.TryParse(str, out result);

            return result;
        }
    }
}